package pe.edu.upc.dsd.mercadolibre;

import java.util.ArrayList;
import java.util.Collection;



import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class ArticuloDAO extends BaseDAO{


	
	public Collection<Articulo> ListaArticulosDB() throws DAOExcepcion {
		System.out.println("Listar: Buscar Artiuculos()");
		Collection<Articulo> listaArticulos = new ArrayList<Articulo>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			String query = "select * from producto";
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			while (rs.next()) {
				Articulo objInstr = new Articulo();
				objInstr.setId(rs.getInt("idproducto"));
				objInstr.setNombre(rs.getString("nomProd"));
				objInstr.setDescripcion(rs.getString("descProd"));
				objInstr.setStock(rs.getInt("stockProd"));
				objInstr.setPrecio(rs.getDouble("precProd"));
				objInstr.setImagen(rs.getString("imagProd"));
				objInstr.setVendedor(rs.getString("idVendedor"));
				;
				
				listaArticulos.add(objInstr);
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return listaArticulos;
	}
	
	public Collection<Articulo> BuscarArticulosDB(String TxtB) throws DAOExcepcion {
		System.out.println("Listar: Buscar Artiuculos()");
		Collection<Articulo> listaArticulos = new ArrayList<Articulo>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			String query = "select * from producto where nomProd like ? ";
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, "%" + TxtB + "%");
			rs = stmt.executeQuery();
			
			
			while (rs.next()) {
				Articulo objInstr = new Articulo();
				objInstr.setId(rs.getInt("idproducto"));
				objInstr.setNombre(rs.getString("nomProd"));
				objInstr.setDescripcion(rs.getString("descProd"));
				objInstr.setStock(rs.getInt("stockProd"));
				objInstr.setPrecio(rs.getDouble("precProd"));
				objInstr.setImagen(rs.getString("imagProd"));
				objInstr.setVendedor(rs.getString("idVendedor"));
				;
				
				listaArticulos.add(objInstr);
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return listaArticulos;
	}
	
	
	public Articulo ObtenerArticuloDB(int id) throws DAOExcepcion {
		System.out.println("Listar: Buscar Artiuculos()");
		Articulo ArticuloB = new Articulo();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			String query = "select * from producto where idproducto = ?";
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, id);
			rs = stmt.executeQuery();
			
			if (rs.next()) {
				ArticuloB.setId(rs.getInt("idproducto"));
				ArticuloB.setNombre(rs.getString("nomProd"));
				ArticuloB.setDescripcion(rs.getString("descProd"));
				ArticuloB.setStock(rs.getInt("stockProd"));
				ArticuloB.setPrecio(rs.getDouble("precProd"));
				ArticuloB.setImagen(rs.getString("imagProd"));
				ArticuloB.setVendedor(rs.getString("idVendedor"));
				
			}
			
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		
		return ArticuloB;
	}
	
}
